﻿#include <iostream>
#include <set>
#include <map>
#include <string>
#include <ctime>
#include <exception>

using namespace std;

string get_a_target_string()
{
	long target = 0;
	char buf[10];

	cout << "target (0~" << RAND_MAX << "):";
	std::cin >> target;
	snprintf(buf, 10, "%d", target);

	return string(buf);

}

long get_a_target_long()
{
	long target = 0;
	cout << "target (0~" << RAND_MAX << "):";
	std::cin >> target;
	return target;
}

int compareStrings(const void * a, const void * b)
{
	if (*(string*)a > *(string*)b)
		return 1;
	else if (*(string*)a < *(string*)b)
		return -1;
	else
		return 0;
}

void test_multiSet(long & value)
{
	cout << " \n test_multiset..... \n";

	multiset<string> c;
	char buf[10];
	clock_t timeStart = clock();
	for (long i = 0; i < value; i++)
	{
		try {
			snprintf(buf, 10, "%d", rand());
			c.insert(string(buf));
		}
		catch (std::exception& e) {
			cout << "i = " << i << "\t" << e.what() << endl;
		}

	}

	cout << " milli-seconds: " << clock() - timeStart << endl;
	cout << "multiset size:" << c.size() << endl;
	cout << " multiset max_size:" << c.max_size() << endl;
	string target = get_a_target_string();
	timeStart = clock();
	{
		auto item = ::find(c.begin(), c.end(), target);
		cout << "::find" << (clock() - timeStart) << endl;

		if (item != c.end()) {
			cout << "found, " << *item << endl;
		}
		else {
			cout << "not found !" << endl;
		}
	}

	timeStart = clock();
	auto item = c.find(target);
	cout << "multiset  find ,milliseconds: " << (clock() - timeStart) << endl;
	if (item != c.end()) {
		cout << "found, " << *item << endl;
	}
	else {
		cout << "not found !" << endl;
	}
}

void test_multimap(long & value)
{
	cout << "\n test_multimap.... \n";

	multimap<long, string> c;
	char buf[10];
	clock_t timeStart = clock();
	for (long i = 0; i < value; i++)
	{
		try {
			snprintf(buf, 10, "%d", rand());
			c.insert(pair<long, string>(i, buf));//pair 是一个模板 
		}
		catch (std::exception& e) {
			cout << "i = " << i << "\t" << e.what() << endl;
		}
	}

	cout << " milli-seconds: " << clock() - timeStart << endl;
	cout << "multiset size:" << c.size() << endl;
	cout << " multiset max_size:" << c.max_size() << endl;
	long target = get_a_target_long();

	timeStart = clock();
	auto item = c.find(target);
	cout << "multiset  find ,milliseconds: " << (clock() - timeStart) << endl;
	if (item != c.end()) {
		cout << "found, " << (*item).second << endl;
	}
	else {
		cout << "not found !" << endl;
	}

}

void test_map(long & value)
{
	map<long, string> m;
	char buf[10];
	for (long i = 0; i < value; i++)
	{
		try
		{
			snprintf(buf, 10, "%d", rand());
			m[i] = string(buf);
		}
		catch (const std::exception& p)
		{
			cout << "i = " << i << "\t" << p.what() << endl;
			abort();
		}
	}
}

int main(void)
{
	long value = 100000;
	//test_multiSet(value);
	//test_multimap(value);
	allocator<int> alloc1;
	int * p = alloc1.allocate(1);//int * std::allocator<int>::allocate(size_ count);   _Ty * allocate( const size_t _Count)
	alloc1.deallocate(p, 10);//void std::allocator<int>::deallocate(int* _ptr, size_t Count);  void deallocate(_Ty * const _Ptr, const size_t _Count)

	return 0;
}